using System;
using System.Runtime.InteropServices;

namespace WowAceUpdater
{
    internal static class NativeMethods
    {
        #region Constants

        private const int FO_DELETE = 3;

        private const int FOF_ALLOWUNDO = 0x0040;

        private const int FOF_NOCONFIRMATION = 0x0010;

        private const int FOF_NOERRORUI = 0x0400;

        private const int FOF_NORECURSEREPARSE = 0x8000;

        private const int FOF_SILENT = 0x0004;

        #endregion

        #region Structs

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
        public struct SHFILEOPSTRUCT
        {
            public IntPtr hwnd;
            [MarshalAs(UnmanagedType.U4)]
            public int wFunc;
            public string pFrom;
            public string pTo;
            public ushort fFlags;
            [MarshalAs(UnmanagedType.Bool)]
            public bool fAnyOperationsAborted;
            public IntPtr hNameMappings;
            public string lpszProgressTitle;
        }

        #endregion

        #region Public Methods

        public static int SendToRecycleBin(string filePath)
        {
            SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();
            fileop.wFunc = FO_DELETE;
            fileop.pFrom = filePath + '\0' + '\0';
            fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION |
                            FOF_SILENT | FOF_NORECURSEREPARSE | FOF_NOERRORUI;

           return SHFileOperation(ref fileop);
        }

        #endregion

        #region Private Methods

        [DllImport("shell32.dll", CharSet = CharSet.Auto)]
        private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

        #endregion
    }
}
